window.nav = (function() {  
  var userLogged = false;
  var checkLogged = false;
  
  var showPage = function(hash) {    
    console.log(hash);
    //if (location.hash.substr(1) == hash) return; 
    var page = '';
    if (typeof hash=='undefined') hash = '';
    if (!hash.length) { 
      if (nav.userLogged) hash = 'search';
      else hash = 'search';
    }
    if (hash.indexOf("?") != -1) {
      var n = hash.indexOf("?");
      page = hash.substr(0,n); 
    } else {
      page = hash;
    }
    
    var regexp = /ref([0-9]+)/i;
    if (regexp.test(hash)) {
      matches = hash.match(regexp);
      page = 'ref';    
      lib.refid = parseInt(matches[1]);
      $('#vkReg [name=ref]').val(lib.refid);
      $('#fbReg [name=ref]').val(lib.refid);
    }
    
    var regexp = /user([0-9]+)/i;
    if (regexp.test(hash)) {
      page = 'user';
    }    
    
    var regexp = /offer([0-9]+)/i;
    if (regexp.test(hash)) {
      page = 'offer';
    }    

    location.hash = hash;    
    nav.drawRoof();
    nav.page_action(page);
    $(document).scrollTop(0);    
  }
  
  var page_action = function(page) {
    switch (page) {
      case "search":
        pageAction.search();
        break;
      case "catalog":
        pageAction.catalog();
        break;        
      case "index":
        pageAction.index();
        break;
      case "settings":
        pageAction.settings();
        break;
      case "getmoney":
        pageAction.getmoney();
        break;                        
      case "about":
        pageAction.about();
        break;
      case "help":
        pageAction.help();
        break;
      case "dialogs":
        pageAction.dialogs();
        break;
      case "profile":
        pageAction.profile();
        break;
      case "offerform":
        pageAction.offerform();
        break;                                                       
      case "ref":
        pageAction.ref();
        break;        
      case "user":
        pageAction.user();
        break;
      case "offer":
        pageAction.offer();
        break;                       
    }        
  }
  
  var checkAuth = function() {
		var reqData = {
			id: 1,
			on: 'USER',
			'do': 'GET',
			data: {}
		};
    $.ajax({
       type: "POST",
       url: lib.nodeapi,
       dataType: 'JSON',
       data:{msg: $.toJSON(reqData)},
       async: true,
       success: function(response) {
         nav.userLogged = (response.result)?true:false;
         nav.checkLogged = true;
         if (nav.userLogged) {
            if (response.data.user.name.length) user.name = response.data.user.name;
            user.id = response.data.user.user_id;
            if (response.data.user.fotofile != null)
              user.image = '/s3/foto/'+response.data.user.fotofile;
         }
         nav.showPage(jHash.root());
       }
    });
  }
  
  var drawRoof = function() {    
    if (nav.userLogged) {
      $('.noAuthTopItem').hide();
      $('.authTopItem').show();
      $('.main-content').addClass('auth');
      $('.user-menu').show();
      $('#userNameValue').text(user.name);
      $('#topUserImage').attr('src',user.image);
    } else {
      $('.authTopItem').hide();
      $('.noAuthTopItem').show();    
      $('.user-menu').hide();
      $('.main-content').removeClass('auth');
    }
  }    
  
  var signIn = function() {  
    nav.checkAuth();
  }
  
  return {
      showPage: showPage,
      page_action: page_action,
      checkAuth: checkAuth,
      drawRoof: drawRoof,
      signIn: signIn
  };
})(jQuery);

$(window).bind('hashchange', function () {
  window.nav.showPage(location.hash.substr(1));
});

$(function() {  
  /*$('a').on("click", function(){
    var href = $(this).attr('href');
    if (typeof href != 'undefined') {
      if (href.substr(0, 1) == "#" && href.length>1) {      
        nav.showPage(href.substr(1));
        return false;
      } 
    }
  });*/
  
  if (ws.localmode) {
    nav.userLogged = true;
    nav.checkLogged = true;   
    nav.showPage(jHash.root());  
  } else {
    nav.checkAuth();
  }
});